<template>
  <h1>循环指令</h1>
  <ul>
    <!--循环遍历arr数组-->
    <li v-for="item in arr">{{item}}</li>
  </ul>

  <!--商品表之前练习修改-->
  <table border="1">
    <caption>商品表</caption>
    <tr>
      <th>商品编号</th>
      <th>商品名称</th>
      <th>商品价格</th>
      <th>商品库存</th>
      <th>操作</th>
    </tr>
    <!--index:从0开始-->
    <tr v-for="(item, index) in productArr">
      <td>{{index + 1}}</td>
      <td>{{item.title}}</td>
      <td>{{item.price}}</td>
      <td>{{item.num}}</td>
      <td><button @click="del(index)">删除</button></td>
    </tr>
  </table>
</template>

<script setup>
  import {ref} from "vue";
  const arr = ref(['张三', '李四', '王五']);

  const productArr = ref([
    {title: '小米14', price: 8000, num: 200},
    {title: '华为mate60', price: 6000, num: 500},
    {title: '苹果16', price: 9000, num: 100},
    {title: '魅族', price: 3000, num: 300}
  ]);

  const del = (index)=>{
    if (confirm('是否要删除本行数据?')){
      //splice:从index开始,删除1个元素[总个数]
      productArr.value.splice(index, 1);
    }
  }
</script>


<style scoped>

</style>